package com.ym.b2b.salesman.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.salesman.dao.EmBSalesmanProviderDAO;
import com.ym.b2b.salesman.service.EmBSalesmanProviderService;
import com.ym.b2b.salesman.sqlbean.EmBSalesmanProvider;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmBSalesmanProviderServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-3-30 上午10:00:21
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmBSalesmanProviderServiceImpl extends BaseService implements EmBSalesmanProviderService {

    private Logger                 logger = Logger.getLogger(getClass());

    @Autowired
    private EmBSalesmanProviderDAO emBSalesmanProviderDAO;
    
    private Logger         dbLog = Logger.getLogger("db");

    @Override
    public Result searchPageEmBSalesmanProvider(Page<EmBSalesmanProvider> page, EmBSalesmanProvider emBSalesmanProvider)
                                                                                                                        throws DataAccessException {
        Result result = new Result();
        try {
            result.setResultPage(emBSalesmanProviderDAO.searchPageEmBSalesmanProvider(emBSalesmanProvider, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public Result insertEmBSalesmanProvider(EmBSalesmanProvider emBSalesmanProvider) throws DataAccessException {
        Result result = checkInputEmBSalesmanProvider(emBSalesmanProvider);
        if (!result.getCode().equals("200")) return result;
        try {
            if (!"".equals(emBSalesmanProvider.getProviderCodeList())) {
                String[] ProviderCodes = emBSalesmanProvider.getProviderCodeList().split(",");
                for (String ProviderCode : ProviderCodes) {
                    emBSalesmanProvider.setProviderCode(ProviderCode);
                    emBSalesmanProviderDAO.insertEmBSalesmanProvider(emBSalesmanProvider);
                }
                dbLog.info("INSERT-->EM_B_SALESMAN_PROVIDER 业务员("+emBSalesmanProvider.getSalesmanId()+")添加客户 " +"  客户编码："+emBSalesmanProvider.getProviderCodeList());
            }
            result.setResult("业务员添加采购商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "业务员添加采购商失败！");

        }
        return result;
    }

    @Override
    public Result deleteEmBSalesmanProvider(EmBSalesmanProvider emBSalesmanProvider) throws DataAccessException {
        Result result = new Result();
        try {
            emBSalesmanProviderDAO.deleteEmBSalesmanProvider(emBSalesmanProvider);
            dbLog.info("DELETE-->EM_B_SALESMAN_PROVIDER 业务员("+emBSalesmanProvider.getSalesmanId()+")删除客户 " +"  数据值："+JSON.toJSONString(emBSalesmanProvider));
            result.setResult("删除业务员供应商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除业务员供应商失败！");
        }

        return result;
    }

    @Override
    public Result deleteMultipleEmBSalesmanProviders(String belongIdList) throws DataAccessException {
        Result result = new Result();
        try {
            emBSalesmanProviderDAO.deleteMultipleEmBSalesmanProviders(belongIdList);
            dbLog.info("DELETE(Multiple-删除)-->EM_B_SALESMAN_PROVIDER "+"ID: "+belongIdList);
            result.setResult("批量删除业务员客户成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量删除业务员客户失败！");
        }

        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmBSalesman
     */
    private Result checkInputEmBSalesmanProvider(EmBSalesmanProvider p) {
        Result result = new Result();
        if (p.getSalesmanId() == null) {
            result.setCodeResult(Result.CODE_100, "缺少salesmanId业务员ID!");
            return result;
        }
        if (StringUtils.isBlank(p.getProviderCodeList())) {
            result.setCodeResult(Result.CODE_100, "缺少providerCode采购商编码!");
            return result;
        }
        return result;

    }

}
